home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_warnings.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2005-10-18  |  3KB  |  97 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. import warnings
  5. import os
  6. import unittest
  7. from test import test_support
  8.  
  9. class WarningMessage:
  10.     '''Holds results of latest showwarning() call'''
  11.     pass
  12.  
  13.  
  14. def showwarning(message, category, filename, lineno, file = None):
  15.     msg.message = str(message)
  16.     msg.category = category.__name__
  17.     msg.filename = os.path.basename(filename)
  18.     msg.lineno = lineno
  19.  
  20.  
  21. class TestModule(unittest.TestCase):
  22.     
  23.     def setUp(self):
  24.         global msg
  25.         msg = WarningMessage()
  26.         self._filters = warnings.filters[:]
  27.         self._showwarning = warnings.showwarning
  28.         warnings.showwarning = showwarning
  29.         self.ignored = _[1]
  30.  
  31.     
  32.     def tearDown(self):
  33.         warnings.filters = self._filters[:]
  34.         warnings.showwarning = self._showwarning
  35.  
  36.     
  37.     def test_warn_default_category(self):
  38.         for i in range(4):
  39.             text = 'multi %d' % i
  40.             warnings.warn(text)
  41.             self.assertEqual(msg.message, text)
  42.             self.assertEqual(msg.category, 'UserWarning')
  43.         
  44.  
  45.     
  46.     def test_warn_specific_category(self):
  47.         text = 'None'
  48.         for category in [
  49.             DeprecationWarning,
  50.             FutureWarning,
  51.             OverflowWarning,
  52.             PendingDeprecationWarning,
  53.             RuntimeWarning,
  54.             SyntaxWarning,
  55.             UserWarning,
  56.             Warning]:
  57.             if category.__name__ in self.ignored:
  58.                 text = 'filtered out' + category.__name__
  59.                 warnings.warn(text, category)
  60.                 self.assertNotEqual(msg.message, text)
  61.                 continue
  62.             text = 'unfiltered %s' % category.__name__
  63.             warnings.warn(text, category)
  64.             self.assertEqual(msg.message, text)
  65.             self.assertEqual(msg.category, category.__name__)
  66.         
  67.  
  68.     
  69.     def test_filtering(self):
  70.         warnings.filterwarnings('error', '', Warning, '', 0)
  71.         self.assertRaises(UserWarning, warnings.warn, 'convert to error')
  72.         warnings.resetwarnings()
  73.         text = 'handle normally'
  74.         warnings.warn(text)
  75.         self.assertEqual(msg.message, text)
  76.         self.assertEqual(msg.category, 'UserWarning')
  77.         warnings.filterwarnings('ignore', '', Warning, '', 0)
  78.         text = 'filtered out'
  79.         warnings.warn(text)
  80.         self.assertNotEqual(msg.message, text)
  81.         warnings.resetwarnings()
  82.         warnings.filterwarnings('error', 'hex*', Warning, '', 0)
  83.         self.assertRaises(UserWarning, warnings.warn, 'hex/oct')
  84.         text = 'nonmatching text'
  85.         warnings.warn(text)
  86.         self.assertEqual(msg.message, text)
  87.         self.assertEqual(msg.category, 'UserWarning')
  88.  
  89.  
  90.  
  91. def test_main(verbose = None):
  92.     test_support.run_unittest(TestModule)
  93.  
  94. if __name__ == '__main__':
  95.     test_main(verbose = True)
  96.  
  97.